package com.mrobuy.jd.api.category;

import com.jd.open.api.sdk.JdClient;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.PropDto;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.PropGroupDto;
import com.jd.open.api.sdk.domain.supplier.ProductManagementService.response.find.PropValueDto;
import com.jd.open.api.sdk.request.supplier.VcItemPropsFindRequest;
import com.jd.open.api.sdk.response.supplier.VcItemPropsFindResponse;
import com.mrobuy.jd.api.JosClient;
import com.mrobuy.jdvc.entity.VCCategoryProp;
import com.mrobuy.jdvc.entity.VCPropValue;

import java.util.ArrayList;
import java.util.List;

/**
 *  获取我的类目列表
 *
 *  jingdong.vc.item.products.find
 *
 *  <url>https://open.jd.com/home/home#/doc/api?apiCateId=86&apiId=1681&apiName=jingdong.vc.item.products.find</url>
 *
 *
 *
 */
public class PropFindClient extends JosClient<List<VCCategoryProp>> {

    private VcItemPropsFindRequest request=new VcItemPropsFindRequest();

    public PropFindClient(JdClient client) {
        super(client);
    }

    @Override
    public List<VCCategoryProp> call() throws Exception {

        List<VCCategoryProp> result = new ArrayList<>();

        VcItemPropsFindResponse response= client.execute(request);
        if ( response.getJosResultDto().getIsSuccess() ) {

            if ( response.getJosResultDto().getResult()!=null ) {
                for (PropGroupDto propGroupDto : response.getJosResultDto().getResult()) {
                    for (PropDto propDto : propGroupDto.getProps()) {
                        VCCategoryProp categoryProp = new VCCategoryProp();
                        categoryProp.setCid(String.valueOf(request.getCategoryLeafId()));
                        categoryProp.setGroupid(String.valueOf(propGroupDto.getId()));
                        categoryProp.setGroupname(propGroupDto.getName());
                        categoryProp.setGrouporder(String.valueOf(propGroupDto.getOrderSort()));

                        categoryProp.setPropid(String.valueOf(propDto.getId()));
                        categoryProp.setProporder(String.valueOf(propDto.getOrderSort()));
                        categoryProp.setPropname(propDto.getName());
                        categoryProp.setRequired(String.valueOf(propDto.getRequired()));

                        categoryProp.setInputtype(String.valueOf(propDto.getInputType()));
                        categoryProp.setAttralias(propDto.getAttrAlias());
                        categoryProp.setValunit(propDto.getValUnit());

                        result.add(categoryProp);

                        if ( propDto.getValues()!=null ) {
                            List<VCPropValue> vcPropValues = new ArrayList<>();
                            for (PropValueDto propValueDto : propDto.getValues()) {
                                VCPropValue propValue = new VCPropValue();
                                propValue.setCid(request.getCategoryLeafId());
                                propValue.setPropid(categoryProp.getPropid());
                                propValue.setPropname(categoryProp.getPropname());

                                propValue.setValueId(propValueDto.getId());
                                propValue.setValueName(propValueDto.getName());
                                propValue.setOrderSort(propValueDto.getOrderSort());
                                propValue.setSelected(String.valueOf(propValueDto.getSelected()));
                                vcPropValues.add(propValue);
                            }
                            categoryProp.setOptions(vcPropValues);
                        }
                    }

                }
            }
        }

        return result;
    }



    public PropFindClient setCid3(int cid3) {
        request.setCategoryLeafId(cid3);
        return this;
    }
}
